package com.vcg.community.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import vc.dubbo.result.DubboResult;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcg.community.dao.CollectlogMapper;
import com.vcg.community.dao.ResourceMapper;
import com.vcg.community.dao.UserMapper;
import com.vcg.community.model.Collectlog;
import com.vcg.community.model.Ratelog;
import com.vcg.community.model.Resource;
import com.vcg.community.model.User;

public class FavoriteServiceImpl implements FavoriteService {

	@Autowired
	ResourceMapper rsDao;
	@Autowired
	CollectlogMapper clDao;	
	@Autowired
	DataSourceTransactionManager txManager;
	@Autowired
	UserMapper userDao;
	@Autowired
	CollectlogMapper collectDao;

	public DubboResult doFavorite(Integer userId, Integer resourceId) {
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

		TransactionStatus status = txManager.getTransaction(def);

		DubboResult resultObject=new DubboResult(0,"",null);
		try{
			Collectlog rlObject=new Collectlog();
			rlObject.setUserId(userId);
			rlObject.setTargetId(resourceId);
			rlObject.setCreatedtime(new Date());
			clDao.insert(rlObject);
		}catch(Exception ex){
			txManager.rollback(status);
			resultObject.setCode(-1);
			resultObject.setCodeError(ex.toString());
			return resultObject;
		}
		txManager.commit(status);
		return resultObject;		

	}
	public DubboResult<List<Resource>> getFavoritedResources(Integer userId){
		DubboResult<List<Resource>> resultObject=new DubboResult<List<Resource>>(0,null);
		try{
			List<Resource> lstResource=rsDao.getFavoritedResources(userId);
			for(int i=0;i<lstResource.size();i++){
				Resource rs=lstResource.get(i);
				rs.setUrl("http://photo-community.shijue.me/"+rs.getResourceId()+".jpg");
			}
			resultObject.setResult(lstResource);
		}catch(Exception ex){
			resultObject.setCode(-1);
			resultObject.setCodeError(ex.toString());
			
		}
		return resultObject;
	}
	public DubboResult<List<User>> getFavoritedUsers(Integer resourceId) {
		DubboResult<List<User>> resultObject=new DubboResult<List<User>>(200,null);
		try{
			List<User> lstUser=userDao.getFavoritedUsersByResourceId(resourceId);
			resultObject.setResult(lstUser);
		}catch(Exception ex){
			resultObject.setCode(521);
			resultObject.setCodeError(ex.toString());
			
		}
		return resultObject;
	}
	public DubboResult doFavorite(String strUserId, String strResourceId) {
		// TODO Auto-generated method stub
		return doFavorite(getUserbyStringId(strUserId), getResourcebyStringId(strResourceId));
	}
	public DubboResult<List<Resource>> getFavoritedResources(String strUserId) {
		// TODO Auto-generated method stub
		return this.getFavoritedResources(getUserbyStringId(strUserId));
	}
	public DubboResult<List<User>> getFavoritedUsers(String strResourceId) {
		// TODO Auto-generated method stub
		return getFavoritedUsers(getResourcebyStringId(strResourceId));
	}
	
	private Integer getUserbyStringId(String strUserId){
		User usr=userDao.SelectUserByStringId(strUserId);
		if(usr!=null){
		return usr.getId();
		}else{
			return -1;
		}
	}
	private Integer getResourcebyStringId(String strResourceId){
		Resource rs=rsDao.selectByResourceID(strResourceId);
		if(rs!=null){
			return rs.getId();
		}else{
			return -1;
		}
	}
	public DubboResult<Map<String, Boolean>> getState(String strUserId,
			String[] strResourceId) {
		DubboResult<Map<String, Boolean>> dbResult=new DubboResult<Map<String, Boolean>>(200,"",null);
		Map<String, Boolean> resultMap=new HashMap<String,Boolean>();
		// TODO Auto-generated method stub
		Integer userId=getUserbyStringId(strUserId);
//		List<Integer> resourceIdList=new ArrayList<Integer>();
		for(String strRSId:strResourceId){
//			resourceIdList.add(getResourcebyStringId(strRSId));
			Collectlog clRecord=collectDao.getCollectState(userId, getResourcebyStringId(strRSId));
			
			if(clRecord!=null){
				resultMap.put(strRSId, true);
			}else{
				resultMap.put(strRSId, false);				
			}			
		}
		dbResult.setResult(resultMap);
		return dbResult;
	}
	public DubboResult<Map<String, Long>> getCount(String[] strResourceId) {
		DubboResult<Map<String, Long>> dbResult=new DubboResult<Map<String, Long>>(200,"",null);
		Map<String, Long> resultMap=new HashMap<String,Long>();
  	    for(String strRSId:strResourceId){
  	    	Integer intRSId=getResourcebyStringId(strRSId);
  	    	if(intRSId==-1){
  	    		resultMap.put(strRSId, (long) 0);
  	    	}else{
  	    		Long cnt=collectDao.getCollectCount(intRSId);
  	    		resultMap.put(strRSId, cnt);
  	    	}
		}
  	    dbResult.setResult(resultMap);
		return dbResult;
	}

}
